package com.avic.rong.activity.product;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.activity.product.api.UserCommentApi;
import com.avic.rong.adapter.product.CommentListAdapter;
import com.avic.rong.bean.product.CommentBean;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Creater: Created by wangyz on 2019/8/14.
 * Email: 936804097@qq.com
 */
public class CommentListActivity extends BaseActivity implements OnRefreshListener,OnLoadMoreListener {


    public static final String ARG_COMMENT_TYPE = "commentType";
    public static final String ARG_GOOD_ID  = "good_id";


    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.recycler_list)
    RecyclerView recycler_list;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;


    private CommentListAdapter commentListAdapter;
    private List<CommentBean.CommentsBean> commentsBeanList = new ArrayList<>();
    private UserCommentApi userCommentApi;

    private Integer commentType = 3;
    private Integer goodId = 0;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_comment_list;
    }


    @Override
    protected void initView() {

        ButterKnife.bind(this);

        commentType = getIntent().getIntExtra(ARG_COMMENT_TYPE,3);
        goodId = getIntent().getIntExtra(ARG_GOOD_ID, 0);


        commentListAdapter = new CommentListAdapter(CommentListActivity.this, commentsBeanList);
//        DividerItemDecoration divider = new DividerItemDecoration(CommentListActivity.this, DividerItemDecoration.VERTICAL);
//        divider.setDrawable(ContextCompat.getDrawable(CommentListActivity.this, R.drawable.divider_10_pix));
//        recycler_list.addItemDecoration(divider);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CommentListActivity.this);
        recycler_list.setLayoutManager(linearLayoutManager);
        recycler_list.setAdapter(commentListAdapter);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);


        userCommentApi = new UserCommentApi(CommentListActivity.this,commentListener);
        userCommentApi.setArgument(commentType,goodId);
        HttpManager.getInstance().doHttpDeal(userCommentApi);

        loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                HttpManager.getInstance().doHttpDeal(userCommentApi);
                loading_layout.setStatus(LoadingLayout.LOADING);
            }

            @Override
            public void onReturn(View v) {
                onBackPressed();
            }
        });
        loading_layout.setStatus(LoadingLayout.LOADING);

    }

    HttpOnNextListener<BaseResultEntity> commentListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                ToastTools.showToast(CommentListActivity.this, subjects.getMsg());
                return;
            }
            CommentBean commentBean = (CommentBean)subjects.getData();
            commentsBeanList = commentBean.getComments();
            if (isRefresh){
                commentListAdapter.clearData();
            }
            commentListAdapter.setCommentsBeanList(commentsBeanList);
            currentPageNumber = commentBean.getPageNo();

            if (commentListAdapter.getCommentsBeanList() == null || commentListAdapter.getCommentsBeanList().size() <= 0){
                loading_layout.setStatus(LoadingLayout.EMPTY);
            }else{
                loading_layout.setStatus(LoadingLayout.SUCCESS);
            }


            if (isRefresh){
                refreshLayout.finishRefresh();
            }else{
                refreshLayout.finishLoadMore();
            }

//            loading_layout.setStatus(LoadingLayout.EMPTY);
//            loading_layout.setStatus(LoadingLayout.ERROR);
        }

        @Override
        public void onError(final Throwable e) {
            if (isRefresh){
                refreshLayout.finishRefresh();
            }else{
                refreshLayout.finishLoadMore();
            }
            loading_layout.setStatus(LoadingLayout.ERROR);
        }
    };





        @Override
    protected void addListener() {

    }

    @OnClick(R.id.img_back)
    public void backClick(){
        onBackPressed();
    }


    @OnClick(R.id.tv_ok)
    public void dicrectGo(View view){
        showPopup(CommentListActivity.this,findViewById(R.id.ll_root_top));
    }




    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        isRefresh = false;
        currentPageNumber = currentPageNumber + 1;
        userCommentApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(userCommentApi);

//        refreshLayout.finishLoadMore(2000);//传入false表示加载失败
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        isRefresh = true;
        currentPageNumber = 1;
        userCommentApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(userCommentApi);

//        refreshLayout.finishRefresh(2000);//传入false表示刷新失败
    }



    public static Intent getIntent(Context context,Integer commentType, Integer goodId){
        Intent intent = new Intent(context, CommentListActivity.class);
        intent.putExtra(ARG_GOOD_ID, goodId);
        intent.putExtra(ARG_COMMENT_TYPE, commentType);
        return intent;
    }

}
